package org.jypj.dev.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;

/**
 * chenyu
 */
public class SessionListener implements HttpSessionListener {

    //临时的sessionMap userName+mac->MD5加密
    public static HashMap<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();

    //浏览器创建的session key userName+mac->MD5加密
    public static HashMap<String, HttpSession> realSessionMap = new HashMap<String, HttpSession>();

    //连接被建立的时间 key userName+mac->MD5加密
    public static HashMap<String, Long> timeMap = new HashMap<>();

    /**
     * session被创建时触发
     *
     * @param se
     */
    public void sessionCreated(HttpSessionEvent se) {


    }

    /**
     * session被销毁时触发
     *
     * @param se
     */
    public void sessionDestroyed(HttpSessionEvent se) {


    }

    /**
     * @return
     * @author
     * @description 强行把已经在线的用户的session注销
     */
    public static void forceLogoutSessionMapUser(String key) {
        // 删除单一登录中记录的变量
        HttpSession httpSession = SessionListener.sessionMap.get(key);
        if (httpSession != null) {
            SessionListener.sessionMap.remove(key);
            httpSession.invalidate();
        }
    }

    /**
     * @return
     * @author
     * @description 强行把已经在线的用户的session注销
     */
    public static void forceLogoutRealSessionMapUser(String key) {
        // 删除单一登录中记录的变量
        HttpSession httpSession = SessionListener.realSessionMap.get(key);
        if (httpSession != null) {
            SessionListener.realSessionMap.remove(key);
            httpSession.invalidate();
        }
    }
}
